/**
 * Created by Dell on 2017.10.10.
 */
var con_select ;
var reg_select ;
var chr_select ;
var chrsy_select ;
var regListsSult;
var chrListsSult;
var chrListsSult_sy;

var chartText;
var chartName;
var chartData;
var chartpointFormat;
var chartData_num = []; //个数
var chartData_Str = []; //总建筑面积
var chartData_use = []; //自用面积
var chartData_Floor = []; // 占地面积
var chartData_Book = []; //账面价值
var chartData_Rental1 = [];//出租面积
var chartData_Idle = [];//闲置面积
var chartData_Invest = [];//对外投资面积
var chartData_Mortgage = [];//抵押担保面积
var chartData_Other = [];//抵押担保面积
var thisVal;
var MJselect;
var MJtype = '占地面积';
var conType = 1;
var GroupField = "Region_ID";
var Region_IDval;
var CHR_IDval;
var DWType_IDval;
var regVal = '000';
var chrVal = '071';
var chrsyVal;

var Token = '123'
$(function(){
    con_select = $('#con_select').customSelect({
        width:158,
        lineHeight:28
    });
    chrsy_select = $('#chrsy_select').customSelect({
        width:250,
        lineHeight:28,
        show:false
    });
    // 单位性质
    DWType_select = $('#DWType_select').customSelect({
        width:158,
        lineHeight:28,
        show:false
    });

    var tjdbHeight = $('.contrastMain').height()-$('.selectBox').height()-$('.chartsBox').height();
    $('.tjdb ').height(tjdbHeight);
    $('.dbList,#chartDB').height(tjdbHeight-$('.tjdb .tit').height()-20);
    $('.chrLists').height($('.dbList').height()-$('.dbBtn').height()-10);
    var str;
    getRegSelect();
    getChrSelect();
    con_select.change(function(){
        conType = con_select.getValue();
        if(conType == 1){
            reg_select.show();
            chr_select.hide();
            chrsy_select.hide();
            DWType_select.hide();
            $('.chrLists').html(regListsSult);
        }
        else if(conType == 2){
            chr_select.show();
            reg_select.hide();
            DWType_select.hide();
            if(chr_select.getValue() != '071') {
                chrsy_select.show();
                $('.chrLists').html(chrListsSult_sy);
            } else {
                chrsy_select.hide();
                $('.chrLists').html(chrListsSult);
            }
            chr_select.change(function(){
                chrVal = chr_select.getValue();
                if(chrVal != '071'){
                    chrsy_select.show();
                    getChr_sySelect();
                }else {
                    chrsy_select.hide();
                    $('.chrLists').html(chrListsSult);
                }
            });

        }
        else{
            reg_select.hide();
            chr_select.hide();
            chrsy_select.hide();
            DWType_select.show();
            $('.chrLists').html('<li><input type="checkbox" name="staID" value="100">行政单位</li>' +
                '<li><input type="checkbox" name="staID" value="200">国有企业</li>' +
                '<li><input type="checkbox" name="staID" value="301">财政补助事业单位</li>' +
                '<li><input type="checkbox" name="staID" value="302">参照公务员法管理事业单位</li>' +
                '<li><input type="checkbox" name="staID" value="303">经费自理事业单位</li>' +
                '<li><input type="checkbox" name="staID" value="999">其他</li>');
        }
    });


    DWType_select.change(function(){
        if(DWType_select.getValue() != '000'){

        }

    })


    MJselect = $('#MJselect').customSelect({
        width:158,
        lineHeight:28,
        show:false
    });

    $('input[name="DBtype"]').change(function(){
        thisVal = $(this).val();
        thisVal == 3 ? MJselect.show(): MJselect.hide();
    });
    $('input[name="DBtype"]').eq(0).trigger('change');

    MJselect.change(function(){
        MJtype = MJselect.getValue();
    });



    // 对比分析 查询
    $('.queryBtn').click(function(){
        var regId;
        var chrId;
        var DWTypeId;
        if(conType == 1){
            //console.log(regVal)
            regId = regVal;
            chrId = '';
            DWTypeId = '';
        }else if(conType == 2){
            regId = '';
            if(chrVal != '071'){
                chrId = chrsyVal
            }else {
                chrId = chrVal;
            }
        }
        else{
            regId = '';
            chrId = '';
            DWTypeId = DWType_select.getValue();
        }
        getPieChart(regId,chrId,DWTypeId)
    });
    $('.queryBtn').trigger('click');


    // 统计对比
    $('.dbBtn').click(function(){
        str = '';
        $('.chrLists').find("[name='staID']").each(function(){
            if ( $(this).is( ":checked" ) ){
                str+=$(this).val()+",";
            }
        });
        str=str.substring(0,str.length-1);

        if(thisVal == 1){
            chartText = '个数 (个)';
            chartName = '个数';
            chartpointFormat = '个数: <b>{point.y:.0f} 个</b>'
        }else if(thisVal == 2){
            chartText = '账面价值 (元)';
            chartName = '账面价值';
            chartpointFormat = '价值（元）: <b>{point.y:.0f} 元</b>'
        }else {
            //chartText = '占地面积 (平方米)';
            //chartName = '占地面积';
            //chartpointFormat = '占地面积: <b>{point.y:.0f} 平方米</b>'

            if(MJtype == '占地面积'){
                chartText = '占地面积 (平方米)';
                chartName = '占地面积';
                chartpointFormat = '占地面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '自用面积'){
                chartText = '自用面积 (平方米)';
                chartName = '自用面积';
                chartpointFormat = '自用面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '出租面积'){
                chartText = '出租面积 (平方米)';
                chartName = '出租面积';
                chartpointFormat = '出租面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '闲置面积'){
                chartText = '闲置面积 (平方米)';
                chartName = '闲置面积';
                chartpointFormat = '闲置面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '对外投资面积'){
                chartText = '对外投资面积 (平方米)';
                chartName = '对外投资面积';
                chartpointFormat = '对外投资面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '抵押担保面积'){
                chartText = '抵押担保面积 (平方米)';
                chartName = '抵押担保面积';
                chartpointFormat = '抵押担保面积: <b>{point.y:.0f} 平方米</b>'
            }else if(MJtype == '抵押担保面积'){
                chartText = '抵押担保面积 (平方米)';
                chartName = '抵押担保面积';
                chartpointFormat = '抵押担保面积: <b>{point.y:.0f} 平方米</b>'
            }else{
                chartText = '其他面积 (平方米)';
                chartName = '其他面积';
                chartpointFormat = '其他面积: <b>{point.y:.0f} 平方米</b>'
            }
        }
        if(conType == 1){
            GroupField = 'Region_ID';
            Region_IDval= str;
            CHR_IDval = "";
            DWType_IDval = ""
        }else if(conType == 2){
            GroupField = 'CHR_ID';
            Region_IDval="";
            CHR_IDval = str;
            DWType_IDval = ""
        }else{
            GroupField = 'DWType_ID';
            Region_IDval="";
            CHR_IDval = "";
            DWType_IDval = str
        }
        //console.log(GroupField)
        //console.log(Region_IDval)
        //console.log(CHR_IDval)
        //console.log(str)
        //console.log(DWType_IDval);

        test_Stat()
    });







});
// 得到乡镇
function getRegSelect (){
    $.post(config.serviceUrl+ "/api/Region", {
            "DATA": {
                "ID": "000",
                "Token" : Token,
            }
        },
        function (jsonData) {
            if(jsonData !=null){
                var reg = jsonData.Region;
                var regsult = '';
                regListsSult = '';
                for(var i=0; i<reg.length;i++){
                    regsult +='<option value="'+ reg[i].Region_ID+'">'+ reg[i].Region_Name +'</option>';
                    regListsSult += '<li><input type="checkbox" name="staID" checked value="'+ reg[i].Region_ID +'">'+ reg[i].Region_Name +'</li>'
                }
                reg_select = $('#reg_select').append(regsult).customSelect({
                    width:158,
                    lineHeight:28,
                });
                $('.chrLists').html(regListsSult);
                $('.dbBtn').trigger('click');
                reg_select.change(function(){
                    regVal = reg_select.getValue();
                });
                reg_select.trigger('change')
            }
        }
    );
}

// 得到主管单位
function getChrSelect (){
    $.post(config.serviceUrl+ "/api/Chr", {
            "DATA": {
                "Prefix": "071",
                "Token" : Token,
            }
        },
        function (jsonData) {
            //console.log(jsonData)
            if(jsonData !=null){
                var chr = jsonData.CHR;
                var chrsult = '';
                chrListsSult = '';
                for(var i=0; i<chr.length;i++){
                    chrsult +='<option value="'+ chr[i].CHR_ID+'">'+ chr[i].CHR_Name +'</option>';
                    chrListsSult += '<li><input type="checkbox" name="staID"  value="'+ chr[i].CHR_ID +'">'+ chr[i].CHR_Name +'</li>'
                }
                chr_select = $('#chr_select').append(chrsult).customSelect({
                    width:250,
                    lineHeight:28,
                    show:false
                });
                chr_select.trigger('change')
            }
        }
    );
}

// 得到使用单位
function getChr_sySelect (){
    $.post(config.serviceUrl+ "/api/Chr", {
            "DATA": {
                "Prefix": chrVal,
                "Token" :Token,
                "async":true
            }
        },
        function (jsonData) {
            console.log(jsonData);
            if(jsonData !=null){
                if(jsonData.CHR[0].subCHR){
                    var chrsy = jsonData.CHR[0].subCHR;
                    console.log(chrsy)
                    chrListsSult_sy ='';
                    var chrsysult = ' <option value="'+chrVal+'">全部使用单位</option>';
                    for(var i=0; i<chrsy.length;i++){
                        chrsysult +='<option value="'+ chrsy[i].CHR_ID+'">'+ chrsy[i].CHR_Name +'</option>';
                        chrListsSult_sy += '<li><input type="checkbox" name="staID" value="'+ chrsy[i].CHR_ID +'">'+ chrsy[i].CHR_Name +'</li>';
                    }
                    $('#chrsy_select').prev('.select_box').remove();
                    chrsy_select = $('#chrsy_select').html(chrsysult).customSelect({
                        width:158,
                        lineHeight:28,
                    });
                    $('.chrLists').html(chrListsSult_sy);
                    chrsy_select.change(function(){
                        chrsyVal = chrsy_select.getValue();
                    });
                    chrsy_select.trigger('change');
                }
            }
        }
    );
}


// 统计对比图表
function getContrastChart (){
    $('#chartDB').highcharts({
        chart: {
            type: 'column'
        },
        title:false,
        credits: {
            enabled: false
        },
        xAxis: {
            type: 'category',
            labels: {
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text:chartText //'个数 (个)'
            }
        },
        legend: {
            enabled: false
        },
        tooltip: {
            pointFormat:chartpointFormat// '总个数: <b>{point.y:.1f} 个</b>'
        },
        series: [{
            name: chartName, //'总个数',
            data: chartData,
        }]
    });
    $('.tips').hide();
}

function getPieChart (regId,chrId,DWTypeId){
    test_ContrastQQ(regId,chrId,DWTypeId);
    test_ContrastZD(regId,chrId,DWTypeId);
    test_ContrastJZ(regId,chrId,DWTypeId);
    test_ContrastSY(regId,chrId,DWTypeId);
}


// 统计对比
function test_Stat() {
    url = config.serviceUrl + "/api/Fa";
    $.post(url, {
            "DATA": {
                "Action": "Statistics",
                "Region_ID": Region_IDval,     //可省略
                "CHR_ID": CHR_IDval,      //可省略
                "DWType_ID": DWType_IDval,      //可省略
                "GroupField" : GroupField, //Region_ID,CHR_ID
                "Token":Token
            }
        },
        function (jsondata) {
            if(jsondata.FAStatistcs){
                var fa = jsondata.FAStatistcs;
                //console.log(fa);
                chartData_num = [];
                chartData_Str = [];
                chartData_use = [];
                chartData_Floor = [];
                chartData_Book = [];
                chartData_Rental1 = [];
                chartData_Idle = [];
                chartData_Invest = [];
                chartData_Mortgage = [];
                chartData_Other = [];
                if(fa.length > 0){
                    for(var i=0; i<fa.length; i++){
                        var Gname = fa[i].GroupName;
                        if(Gname == ''){
                            Gname = '其他'
                        }
                        var data_num = [Gname,fa[i].Num];
                        chartData_num.push(data_num);
                        var data_Str = [Gname,fa[i].AreaOfStructure]; //占地面积
                        chartData_Str.push(data_Str);
                        var data_use = [Gname,fa[i].AreaOfSelfuse]; //自用面积
                        chartData_use.push(data_use);
                        var data_Floor = [Gname,fa[i].AreaOfFloor];//建筑面积
                        chartData_Floor.push(data_Floor);
                        var data_Book = [Gname,fa[i].BookValue];//账面价值
                        chartData_Book.push(data_Book);
                        var data_Rental1 = [Gname,fa[i].AreaOfRental1];//出租面积
                        chartData_Rental1.push(data_Rental1);
                        var data_Idle = [Gname,fa[i].AreaOfIdle1];//闲置面积
                        chartData_Idle.push(data_Idle);
                        var data_Invest = [Gname,fa[i].AreaOfInvest1];//对外投资面积
                        chartData_Invest.push(data_Invest);
                        var data_Mortgage = [Gname,fa[i].AreaOfMortgage1];//抵押担保面积
                        chartData_Mortgage.push(data_Mortgage);
                        var data_Other = [Gname,fa[i].AreaOfOthers1];//其他面积
                        chartData_Other.push(data_Other);
                    }
                    if(thisVal == 1){
                        chartData = chartData_num
                    }else if(thisVal == 2){
                        chartData = chartData_Book
                    }else {
                        if(MJtype == '占地面积'){
                            chartData = chartData_Floor
                        }else if(MJtype == '自用面积'){
                            chartData = chartData_use
                        }else if(MJtype ==  '建筑面积'){
                            chartData = chartData_Str
                        }else if(MJtype ==  '出租面积'){
                            chartData = chartData_Rental1
                        }else if(MJtype ==  '闲置面积'){
                            chartData = chartData_Idle
                        }else if(MJtype ==  '对外投资面积'){
                            chartData = chartData_Invest
                        }else if(MJtype ==  '抵押担保面积'){
                            chartData = chartData_Mortgage
                        }else if(MJtype ==  '其他面积'){
                            chartData = chartData_Other
                        }
                    }
                    //console.log(chartData);
                    getContrastChart();
                    //$('.dbBtn').trigger('click')
                }
            }else{
                $('#chartDB').html('<p class="tips" style="text-align: center;line-height: 200px;" >暂无数据</p>')
            }
        }
    );
}

// 权情证况
function test_ContrastQQ(regId,chrId,DWTypeId){
    url = config.serviceUrl+ "/api/Fa";
    $.post(url, {
            "DATA": {
                "Action": "Statistics",
                "Region_ID": regId,
                "CHR_ID": chrId,
                "DWType_ID": DWTypeId,
                "GroupField" : "PropertyStatus",
                "Token":Token
            }
        },
        function (jsondata) {
            var fa = jsondata.FAStatistcs;
            if(fa){
                var data = [];
                var str ='';
                for(var i=0;i<fa.length;i++){
                    if(fa[i].GroupName.length != 0){
                        var type = [fa[i].GroupName,fa[i].Num];
                        data.push(type);
                        str += '<p>'+fa[i].GroupName+'<span>'+fa[i].Num+'</span>个</p>'
                    }
                }
                $('.chartQQ').html(str);
                $('#chartQQ').highcharts({
                    chart: {
                        margin: [0, 0, 0, 0],
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    colors:['#11aed0','#41c8e5','#66e0fa'],
                    title: false,
                    credits: {
                        enabled: false
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: false,
                            cursor: 'pointer',
                            dataLabels: false
                        }
                    },
                    series: [{
                        type: 'pie',
                        name: '个数',
                        data: data
                    }]
                });

            }else {
                $('.chartQQ').html('<b style="line-height: 150px;color: #4c4c4c">暂无数据</b>');
                $('#chartQQ').empty();
            }

        }
    )
}

// 单位性质
function test_ContrastZD(regId,chrId,DWTypeId){
    url = config.serviceUrl+ "/api/Fa";
    $.post(url, {
            "DATA": {
                "Action": "Statistics",
                "Region_ID": regId,
                "CHR_ID": chrId,
                "DWType_ID": DWTypeId,
                "GroupField" : "DWType_Name",
                "Token":Token
            }
        },
        function (jsondata) {
            var fa = jsondata.FAStatistcs;
            if(fa){
                var data = [];
                var str ='';
                for(var i=0;i<fa.length;i++){
                    if(fa[i].GroupName == ""){
                        var type = ['其他',fa[i].Num];
                        data.push(type);
                        str += '<p>其他<span>'+fa[i].Num+'</span>个</p>'
                    }else{
                        var type = [fa[i].GroupName,fa[i].Num];
                        data.push(type);
                        str += '<p>'+fa[i].GroupName+'<span>'+fa[i].Num+'</span>个</p>'
                    }
                }
                $('.chartZD').html(str);
                $('#chartZD').highcharts({
                    chart: {
                        margin: [0, 0, 0, 0],
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    colors:['#18b55b','#2fce73','#4cdb89','#85f6b5'],
                    title: false,
                    credits: {
                        enabled: false
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: false,
                            cursor: 'pointer',
                            dataLabels: false
                        }
                    },
                    series: [{
                        type: 'pie',
                        name: '个数',
                        data: data
                    }]
                });
            }else {
                $('.chartZD').html('<b style="line-height: 150px;color: #4c4c4c">暂无数据</b>');
                $('#chartZD').empty();
            }


        }
    )
}

// 用地性质
function test_ContrastJZ(regId,chrId,DWTypeId){
    url = config.serviceUrl+ "/api/Fa";
    $.post(url, {
            "DATA": {
                "Action": "Statistics",
                "Region_ID": regId,
                "CHR_ID": chrId,
                "DWType_ID": DWTypeId,
                "GroupField" : "LandType_Name",
                "Token":Token
            }
        },
        function (jsondata) {
            //console.log(jsondata);
            var fa = jsondata.FAStatistcs;
            if(fa){
                var data = [];
                var str ='';
                var GYSum = 0;
                var SYSum = 0;
                var otherSum = 0;
                for(var i=0;i<fa.length;i++){
                    //if(fa[i].GroupName.length != 0) {
                    //    var type = [fa[i].GroupName,fa[i].Num];
                    //    data.push(type);
                    //    str += '<p>'+fa[i].GroupName+'<span>'+fa[i].Num+'</span>个</p>'
                    //}
                    if(fa[i].GroupName.indexOf('工业用地')>=0){
                        GYSum += fa[i].Num;
                    }else if(fa[i].GroupName.indexOf('住宅用地')>=0){
                        SYSum += fa[i].Num;
                    }else{
                        otherSum += fa[i].Num;
                    }
                }
                data.push(['工业用地',GYSum]);
                data.push(['商业用地',SYSum]);
                data.push(['其他',otherSum]);
                str += '<p style="">工业用地'+'<span>'+GYSum+'</span>个</p>';
                str += '<p style="">商业用地'+'<span>'+SYSum+'</span>个</p>';
                str += '<p style="">其他'+'<span>'+otherSum+'</span>个</p>';

                $('.chartJZ').html(str);
                $('#chartJZ').highcharts({
                    chart: {
                        margin: [0, 0, 0, 0],
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    colors:['#ffaa22','#fbb444','#fbbf5f','#fac777','#fad292','#feda9f','#fee2b5'],
                    title: false,
                    credits: {
                        enabled: false
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: false,
                            cursor: 'pointer',
                            dataLabels: false
                        }
                    },
                    series: [{
                        type: 'pie',
                        name: '个数',
                        data: data
                    }]
                });
            }else {
                $('.chartJZ').html('<b style="line-height: 150px;color: #4c4c4c">暂无数据</b>');
                $('#chartJZ').empty();
            }


        }
    )
}

// 在用状态
function test_ContrastSY(regId,chrId,DWTypeId){
    url = config.serviceUrl+ "/api/Fa";
    $.post(url, {
            "DATA": {
                "Action": "Statistics",
                "Region_ID": regId,
                "CHR_ID": chrId,
                "DWType_ID": DWTypeId,
                "GroupField" : "UsingStatus",
                "Token":Token
            }
        },
        function (jsondata) {
            //console.log(jsondata);
            var fa = jsondata.FAStatistcs;
            if(fa){
                var data = [];
                var str ='';
                for(var i=0;i<fa.length;i++){
                    if(fa[i].GroupName.length != 0){
                        var type = [fa[i].GroupName,fa[i].Num];
                        data.push(type);
                        str += '<p>'+fa[i].GroupName+'<span>'+fa[i].Num+'</span>个</p>'
                    }
                }
                $('.chartSY').html(str);
                $('#chartSY').highcharts({
                    chart: {
                        margin: [0, 0, 0, 0],
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false
                    },
                    colors:['#f96262','#fc7171','#fc9292'],
                    title: false,
                    credits: {
                        enabled: false
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: false,
                            cursor: 'pointer',
                            dataLabels: false
                        }
                    },
                    series: [{
                        type: 'pie',
                        name: '个数',
                        data: data
                    }]
                });
            }else {
                $('.chartSY').html('<b style="line-height: 150px;color: #4c4c4c">暂无数据</b>');
                $('#chartSY').empty();
            }


        }
    )
}